<?php

class Response {
    protected $success;
    protected $message;
    protected $data;
    protected $errors;

    public function __construct($success = false, $message = '', $data = null, $errors = null) {
        $this->success = $success;
        $this->message = $message;
        $this->data = $data;
        $this->errors = $errors;
    }

    public static function success($message = '操作成功', $data = null) {
        return new self(true, $message, $data, null);
    }

    public static function error($message = '操作失败', $errors = null) {
        return new self(false, $message, null, $errors);
    }

    public function toJson() {
        return json_encode([
            'success' => $this->success,
            'message' => $this->message,
            'data' => $this->data,
            'errors' => $this->errors
        ]);
    }

    public function send() {
        header('Content-Type: application/json;charset=utf-8');

        header('Access-Control-Allow-Origin: *');
        echo $this->toJson();
        exit;
    }
} 